home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / SymbolTest.java < prev    next >
Text File  |  1998-09-15  |  5KB  |  150 lines

  1. /*
  2.  * @(#)SymbolTest.java    1.1 98/07/18
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. import java.awt.*;
  21. import java.awt.event.*;
  22. import java.applet.Applet;
  23.  
  24. public class SymbolTest extends Applet implements ActionListener, ItemListener {
  25.  
  26.     SymbolCanvas symbols; 
  27.     TextField baseText;
  28.  
  29.     static final int SYMBOL_BASE = 0x2200;
  30.     static final int DINGBAT_BASE = 0x2700;
  31.     static final int GREEK_BASE = 0x3300;
  32.  
  33.     public void init() {
  34.     setLayout(new BorderLayout());
  35.  
  36.         Panel panel = new Panel();
  37.  
  38.         panel.add(new Label("Font:"));
  39.         Choice fontList = new Choice();
  40.         String[] fontNames = getToolkit().getFontList();
  41.         for (int i = 0; i < fontNames.length; i++) {
  42.             fontList.addItem(fontNames[i]);
  43.         }
  44.         fontList.addItemListener(this);
  45.         panel.add(fontList);
  46.         Font defaultFont = new Font(fontNames[0], Font.PLAIN, 16);
  47.  
  48.         panel.add(new Label("Unicode base:"));
  49.         baseText = new TextField(Integer.toHexString(DINGBAT_BASE), 4);
  50.         baseText.setFont(new Font("Monospaced", Font.PLAIN, 12));
  51.         baseText.addActionListener(this);
  52.         panel.add(baseText);
  53.         add("North", panel);
  54.  
  55.         ScrollPane sp = new ScrollPane();
  56.         symbols = new SymbolCanvas(defaultFont, DINGBAT_BASE);
  57.         sp.add(symbols);
  58.         add("Center", sp);
  59.  
  60.         add("South", new Label("Symbols=0x2200, Dingbats=0x2700, Greek=0x3300"));
  61.     }
  62.  
  63.     public void itemStateChanged(ItemEvent e) {
  64.         if (e.getStateChange() == ItemEvent.SELECTED) {
  65.             String fontName = (String)e.getItem();
  66.             symbols.setFont(new Font(fontName, Font.PLAIN, 16));
  67.         }
  68.     }
  69.  
  70.     public void actionPerformed(ActionEvent e) {
  71.         try {
  72.             int newBase = Integer.valueOf(e.getActionCommand(), 16).intValue();
  73.             symbols.setBase(newBase);
  74.         } catch (NumberFormatException nfe) {
  75.             Toolkit.getDefaultToolkit().beep();
  76.             baseText.select(0, Integer.MAX_VALUE);
  77.         }
  78.     }
  79.  
  80.     /*
  81.      * This class demonstrates how adapter classes can be used to avoid
  82.      * creating empty methods to satisfy an event listener interface.
  83.      * Being a nested class, a separate class file won't be created 
  84.      * (which would be overkill for implementing this functionality).
  85.      */
  86.     static class MyAdapter extends WindowAdapter {
  87.         public void windowClosing(WindowEvent e) {
  88.             System.exit(0);
  89.         }
  90.     }
  91.  
  92.     public static void main(String args[]) {
  93.     Frame f = new Frame("SymbolTest");
  94.     SymbolTest symbolTest = new SymbolTest();
  95.  
  96.     symbolTest.init();
  97.     symbolTest.start();
  98.  
  99.     f.add("Center", symbolTest);
  100.     f.pack();
  101.     f.setSize(400, 500);
  102.     f.show();
  103.  
  104.         f.addWindowListener(new MyAdapter());
  105.     }
  106. }
  107.  
  108. class SymbolCanvas extends Canvas {
  109.     Font font;
  110.     int charHeight;
  111.     int charWidth;
  112.     int charBase;
  113.  
  114.     public SymbolCanvas(Font font, int base) {
  115.         FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
  116.         charHeight = fm.getHeight() + 3;
  117.         charWidth = fm.getMaxAdvance() + 4;
  118.         charBase = base;
  119.         setSize(charWidth * 16 + 60, charHeight * 16 + 10);
  120.     }
  121.  
  122.     public void setBase(int base) {
  123.         charBase = base;
  124.         repaint();
  125.     }
  126.  
  127.     public void setFont(Font font) {
  128.         this.font = font;
  129.         repaint();
  130.     }
  131.  
  132.     public void paint(Graphics g) {
  133.         g.setFont(font);
  134.         g.setColor(Color.black);
  135.         char[] carray = new char[1];
  136.         int c = charBase;
  137.         int y = 20;
  138.         for (int v = 0; v < 16; v++) {
  139.             g.drawString(Integer.toHexString(c), 10, y);
  140.             int x = 60;
  141.             for (int h = 0; h < 16; h++) {
  142.                 carray[0] = (char)c++;
  143.                 g.drawChars(carray, 0, 1, x, y);
  144.                 x += charWidth;
  145.             }
  146.             y += charHeight;
  147.         }
  148.     }
  149. }
  150.